Skip to content

fix: [#2052] Correct caption element content model to allow flow content#2058

Merged
capricorn86 merged 1 commit into
capricorn86:masterfrom
atzzCokeK:fix/issue-2052-caption-content-model
Feb 9, 2026
Merged

fix: [#2052] Correct caption element content model to allow flow content#2058
capricorn86 merged 1 commit into
capricorn86:masterfrom
atzzCokeK:fix/issue-2052-caption-content-model

Conversation

@atzzCokeK
Copy link
Copy Markdown
Contributor

@atzzCokeK atzzCokeK commented Feb 6, 2026

Fixes #2052

Problem

The <caption> element was incorrectly configured with contentModel: textOrComments, which only allowed text nodes and comments as children. According to the HTML specification, caption elements should contain flow content (including inline and block elements), except table elements.

This caused inline elements like <b>, <em>, <span>, etc. to be incorrectly moved outside the caption during HTML parsing, resulting in broken DOM structures.

Reproduction

import { Window } from 'happy-dom';

const window = new Window();
window.document.write(`
  <table>
    <caption>
      This <b>is</b> a caption.
    </caption>
  </table>
`);

console.log(window.document.documentElement.outerHTML);
// Before fix: <b></b><table><caption>This is a caption.</caption>...
// After fix:  <table><caption>This <b>is</b> a caption.</caption>...

Changes

packages/happy-dom/src/config/HTMLElementConfig.ts

Updated the caption element configuration to follow the HTML spec and match the pattern used by other table elements (td, th):

caption: {
    className: 'HTMLTableCaptionElement',
    contentModel: HTMLElementConfigContentModelEnum.noForbiddenFirstLevelDescendants,
    forbiddenDescendants: ['table', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th', 'col', 'colgroup'],
    permittedParents: ['table']
}

Changes explained:

  • contentModel: Changed from textOrComments to noForbiddenFirstLevelDescendants to allow flow content
  • forbiddenDescendants: Added table structure elements (per HTML spec: "Flow content, but with no descendant table elements")
  • permittedParents: Added ['table'] to ensure caption only appears as a child of table elements

packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts

Added comprehensive test coverage (9 new tests) for the caption element content model:

  1. Inline elements preservation - Tests that <b>, <strong>, <em>, <span>, <a> are correctly preserved inside caption
  2. Nested inline elements - Tests that nested structures like <small><b>text</b></small> work correctly
  3. Block-level elements - Tests that block elements like <p> and <div> are allowed (flow content)
  4. Table element prohibition - Tests that <table> elements inside caption are correctly moved outside
  5. Content serialization - Tests that caption content is preserved when serializing to HTML
  6. permittedParents validation - Tests that caption tags are removed when parent is not <table> or when used standalone

Implementation Details

This implementation follows the same pattern as PR #2007 (paragraph elements), using the existing noForbiddenFirstLevelDescendants content model. While this approach doesn't recursively check for deeply nested table elements (e.g., <caption><div><table>...</table></div></caption>), it:

  • Matches the established codebase patterns
  • Handles the vast majority of real-world use cases
  • Provides dual protection through both forbiddenDescendants and permittedParents
  • Maintains consistency with other table-related elements

Test Coverage

All tests pass:

✓ test/html-parser/HTMLParser.malformedHTML.test.ts (16 tests) 17ms
  Test Files  1 passed (1)
  Tests  16 passed (16)

Full test suite:

Test Files  294 passed (294)
Tests  7164 passed (7164)

@atzzCokeK atzzCokeK force-pushed the fix/issue-2052-caption-content-model branch from 22e636a to 614b105 Compare February 6, 2026 13:41
@atzzCokeK atzzCokeK marked this pull request as ready for review February 6, 2026 13:50
…w flow content

Fixes capricorn86#2052

## Problem

The caption element was incorrectly configured with contentModel: textOrComments,
which only allowed text nodes and comments. Per the HTML spec, caption elements
should contain flow content (inline and block elements), except table elements.

This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved
outside the caption during parsing.

## Changes

### packages/happy-dom/src/config/HTMLElementConfig.ts

Updated caption element configuration to match the HTML spec and follow the
same pattern as td/th elements:

- Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants
- Added forbiddenDescendants: table structure elements (table, tbody, thead,
  tfoot, tr, td, th, col, colgroup)
- Added permittedParents: ['table'] to ensure caption only appears in tables

### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts

Added comprehensive test coverage for caption element content model:
- Inline elements preservation (b, strong, em, span, a)
- Nested inline elements
- Block-level elements (p, div)
- Table element prohibition
- Content serialization
- permittedParents validation (wrong parent, standalone, correct parent)

## Implementation Details

This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using
the existing noForbiddenFirstLevelDescendants content model. While this
doesn't recursively check deeply nested table elements, it matches the
existing codebase patterns and handles the vast majority of real-world cases.
@atzzCokeK atzzCokeK force-pushed the fix/issue-2052-caption-content-model branch from e9ef366 to 639c2d8 Compare February 6, 2026 13:54
@atzzCokeK
Copy link
Copy Markdown
Contributor Author

atzzCokeK commented Feb 6, 2026

CI Test Failure Note

The failing Fetch test is unrelated to this PR's changes (caption element only). The same failure occurs in #2048 and is linked to #2022/#2023.

All 16 caption-related tests pass successfully in my environment.

@Spixmaster
Copy link
Copy Markdown

Was this done an LLM? Who on earth writes such pull request descriptions?

@atzzCokeK
Copy link
Copy Markdown
Contributor Author

I did use an LLM as a writing aid and reviewed the content myself. Please let me know if you have any feedback on the actual changes👋

Copy link
Copy Markdown
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @atzzCokeK! :star

@capricorn86 capricorn86 merged commit fbef5d9 into capricorn86:master Feb 9, 2026
3 checks passed
RAprogramm pushed a commit to RAprogramm/fork-happy-dom that referenced this pull request Feb 20, 2026
…w flow content (capricorn86#2058)

Fixes capricorn86#2052

## Problem

The caption element was incorrectly configured with contentModel: textOrComments,
which only allowed text nodes and comments. Per the HTML spec, caption elements
should contain flow content (inline and block elements), except table elements.

This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved
outside the caption during parsing.

## Changes

### packages/happy-dom/src/config/HTMLElementConfig.ts

Updated caption element configuration to match the HTML spec and follow the
same pattern as td/th elements:

- Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants
- Added forbiddenDescendants: table structure elements (table, tbody, thead,
  tfoot, tr, td, th, col, colgroup)
- Added permittedParents: ['table'] to ensure caption only appears in tables

### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts

Added comprehensive test coverage for caption element content model:
- Inline elements preservation (b, strong, em, span, a)
- Nested inline elements
- Block-level elements (p, div)
- Table element prohibition
- Content serialization
- permittedParents validation (wrong parent, standalone, correct parent)

## Implementation Details

This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using
the existing noForbiddenFirstLevelDescendants content model. While this
doesn't recursively check deeply nested table elements, it matches the
existing codebase patterns and handles the vast majority of real-world cases.
RAprogramm pushed a commit to RAprogramm/fork-happy-dom that referenced this pull request Feb 20, 2026
…w flow content (capricorn86#2058)

Fixes capricorn86#2052

## Problem

The caption element was incorrectly configured with contentModel: textOrComments,
which only allowed text nodes and comments. Per the HTML spec, caption elements
should contain flow content (inline and block elements), except table elements.

This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved
outside the caption during parsing.

## Changes

### packages/happy-dom/src/config/HTMLElementConfig.ts

Updated caption element configuration to match the HTML spec and follow the
same pattern as td/th elements:

- Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants
- Added forbiddenDescendants: table structure elements (table, tbody, thead,
  tfoot, tr, td, th, col, colgroup)
- Added permittedParents: ['table'] to ensure caption only appears in tables

### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts

Added comprehensive test coverage for caption element content model:
- Inline elements preservation (b, strong, em, span, a)
- Nested inline elements
- Block-level elements (p, div)
- Table element prohibition
- Content serialization
- permittedParents validation (wrong parent, standalone, correct parent)

## Implementation Details

This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using
the existing noForbiddenFirstLevelDescendants content model. While this
doesn't recursively check deeply nested table elements, it matches the
existing codebase patterns and handles the vast majority of real-world cases.
capricorn86 added a commit that referenced this pull request Jun 2, 2026
* fix: [#2035] Updates README.md for the server-renderer package (#2037)

* feat: [#241] Add canvas adapter pattern for pluggable rendering support

Adds ICanvasAdapter interface and static setCanvasAdapter() method to HTMLCanvasElement,
allowing users to plug in real canvas implementations like node-canvas.

Changes:
- Add ICanvasAdapter interface with getContext(), toDataURL(), toBlob() methods
- Add HTMLCanvasElement.setCanvasAdapter() and getCanvasAdapter() static methods
- Delegate canvas operations to adapter when set
- Add NodeCanvasAdapter implementation using node-canvas library
- Add comprehensive tests (44 new tests)

This enables real canvas rendering in Happy-DOM when node-canvas is installed:

```typescript
import { HTMLCanvasElement } from 'happy-dom';
import { NodeCanvasAdapter } from 'happy-dom/lib/nodes/html-canvas-element/adapters/NodeCanvasAdapter.js';

HTMLCanvasElement.setCanvasAdapter(new NodeCanvasAdapter());
// Now canvas.getContext('2d') returns real CanvasRenderingContext2D
```

Closes #241

* fix: [#241] Fix linting errors and code style

- Fix member ordering in HTMLCanvasElement class
- Add curly braces to all if statements
- Add public access modifiers to methods
- Add JSDoc comments for all parameters
- Use angle bracket type assertions in tests
- Fix code formatting per project standards

* fix: [#1850] Selection API focusNode and focusOffset returning incorrect values (#1953)

- Fixed focusNode getter to return the correct focus boundary point
based on selection direction
- Fixed focusOffset getter to return the correct focus offset based on
selection direction

* fix: [#1952] Accept Document nodes as valid boundary points in Selection API (#1954)

* feat: [#2049] Adds support for caching the compiled code of EcmaScript modules (#2050)

* feat: [#2049] Adds support for caching the compiled code of EcmaScript modules

* feat: [#2049] Adds support for caching the compiled code of EcmaScript modules

* feat: [#2049] Adds support for caching the compiled code of EcmaScript modules

* feat: [#2055] Changes internal types to follow a consistent pattern (#2056)

* fix: [#1955] Fixes stepUp and stepDown on HTMLInputElement according to spec (#1960)

* fix: [#1955] Conforming stepUp and stepDown on HTMLInputElement to spec for number inputs

* chore: [#1960] Fixes implementation to follow the spec and the project coding style

* chore: [#1960] Fixes implementation to follow the spec and the project coding style

---------

* fix: [#1947] Use entities package for HTML/XML encoding/decoding (#2016)

- Replace decodeHTMLAttributeValue with entities.decodeHTMLAttribute
- Replace decodeHTMLEntities with entities.decodeHTML
- Replace decodeXMLAttributeValue with entities.decodeXML
- Replace decodeXMLEntities with entities.decodeXML
- Replace encodeTextContent with entities.escapeText
- Add entities@6 as dependency
- Remove unused regex patterns and lookup tables
- Add test for numeric character reference decoding in attributes

This fixes issue #1947 where numeric character references like &#34;
and &#x22; were not being decoded in HTML attribute values.

* chore: [#1947] Adds unit tests for decode &apos; / numeric character references in attr values (#1948)

- attribute values can contain named and numeric character references
  per https://html.spec.whatwg.org/multipage/syntax.html#syntax-attribute-value
- add decoding for all numeric character references
- add decoding for &apos;, this could probably appear in single-quoted
  attribute values
- ignore other named character references, the list is huge

* fix: Node.replaceWith does not throw w/o parent (#1969)

* fix: Node.replaceWith does not throw w/o parent

```js
let div = document.createElement("div");
let span = document.createElement("span");

div.parentNode; // null
span.parentNode; // null

div.replaceWith(span); // ok
```

* chore: remove unused DOMException import

* chore: [#0] Adds unit test

---------

* fix: [#1959] Implement Text.wholeText property (#2027)

* fix: [#1959] Implement Text.wholeText property

* fix: [#1959] Implement Text.wholeText property

---------

* fix: [#2052] Correct caption element content model to allow flow content (#2058)

Fixes #2052

## Problem

The caption element was incorrectly configured with contentModel: textOrComments,
which only allowed text nodes and comments. Per the HTML spec, caption elements
should contain flow content (inline and block elements), except table elements.

This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved
outside the caption during parsing.

## Changes

### packages/happy-dom/src/config/HTMLElementConfig.ts

Updated caption element configuration to match the HTML spec and follow the
same pattern as td/th elements:

- Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants
- Added forbiddenDescendants: table structure elements (table, tbody, thead,
  tfoot, tr, td, th, col, colgroup)
- Added permittedParents: ['table'] to ensure caption only appears in tables

### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts

Added comprehensive test coverage for caption element content model:
- Inline elements preservation (b, strong, em, span, a)
- Nested inline elements
- Block-level elements (p, div)
- Table element prohibition
- Content serialization
- permittedParents validation (wrong parent, standalone, correct parent)

## Implementation Details

This fix follows the same pattern as PR #2007 for paragraph elements, using
the existing noForbiddenFirstLevelDescendants content model. While this
doesn't recursively check deeply nested table elements, it matches the
existing codebase patterns and handles the vast majority of real-world cases.

* feat: [#2060] Adds support for register on import in global-registrator (#2061)

* fix: [#2057] Support Unicode characters in selectors per CSS spec (#2062)

* fix: [#2057] Support Unicode characters in selectors per CSS spec

The CSS specification allows identifiers (class names, IDs, element names)
to contain ISO 10646 characters U+00A0 and higher. The regex patterns in
SelectorParser.ts were only allowing ASCII characters plus a hardcoded
workaround for guillemets.

This change updates both SELECTOR_REGEXP and SIMPLE_SELECTOR_REGEXP to
accept characters in the range \u00A0-\uFFFF, which covers Greek letters,
CJK characters, Cyrillic, Arabic, Hebrew, and other Unicode scripts.

* chore: [#2057] Adds unit tests

* chore: [#2057] Adds unit tests

---------

* fix: [#2042] Support CSS gradients with rgba() colors (#2059)

* fix: [#2042] Support CSS gradients with rgba() colors

This fix resolves an issue where CSS background properties containing
linear gradients with rgba() color values would return empty strings.

Root Cause:
- GRADIENT_REGEXP pattern used [^)]+ which stopped at the first closing
  parenthesis, failing to match gradients with nested rgba() functions
- String splitting logic didn't respect nested parentheses, incorrectly
  splitting rgba(0, 0, 0, 0) into multiple invalid parts

Solution:
- Updated GRADIENT_REGEXP to handle one level of nested parentheses
- Improved getGradient() to split commas while respecting parentheses depth
- Added splitByComma() and splitBySpace() helper functions to properly
  handle nested parentheses when parsing CSS values
- Updated getBackground() and getBackgroundImage() to use new helpers

Tests:
- Added 15 comprehensive test cases covering rgba(), hsla(), rgb(), hsl()
- Tests include repeating gradients, conic gradients, multiple gradients
- Tests verify edge cases: many color stops, decimal values, whitespace
- All 7,184 tests pass

chore: [#2042] Format test file

* fix: [#2042] Improves performance on splitting

* fix: [#2042] Improves performance on splitting

* fix: [#2042] Improves performance on splitting

---------

* fix: [#2066] Update entities package version to resolve missing export for vue and vue-compat v3.5 (#2067)

* fix: [#1910] Fixes issue when parsing complex query selector with has expression (#2068)

* fix: [#1910] Fixes issue when parsing complex CSS has expression

* fix: [#1910] Fixes issue when parsing complex CSS has expression

* fix: [#1910] Fixes issue when parsing complex CSS has expression

* feat: [#241] Refactor canvas adapter to use browser settings

* feat: [#241] Fix `index.ts` missing import

* feat: [#241] Add tests and documentation for node-canvas-adapter

- Add 13 comprehensive tests for CanvasAdapter
- Add .gitignore and .npmignore files
- Enhance README.md with usage examples and API documentation
- Update package.json with scripts, keywords and complete metadata
- Update tsconfig.json with additional compiler options
- Add canvas documentation to happy-dom README

* feat: [#241] Update `package-json.lock`

* feat: [#241] Add canvas adapter pattern for pluggable rendering support

* feat: [#241] Canvas. Fix imports and callback sig

* feat: [#241] Skip canvas tests if canvas not exists

* feat: [#241] Add missing field to mocks

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

* chore: [#0] Continues on implementation

---------

Co-authored-by: David Ortner <david@ortner.se>
Co-authored-by: Sergey Kochetkov <skoch13@icloud.com>
Co-authored-by: Steve Matney <steve.matney@gmail.com>
Co-authored-by: Trevor Burnham <trevorburnham@gmail.com>
Co-authored-by: cgoetz-inovex <carlo.goetz@inovex.de>
Co-authored-by: Luke Edwards <lukeed@github.com>
Co-authored-by: TAKAGI AKIHIRO <156570014+aki05162525@users.noreply.github.com>
Co-authored-by: Atsushi Kawamura (atzz/a2c) <atsushi1230.sdi@gmail.com>
Co-authored-by: Kai Gritun <kaigritun@gmail.com>
Co-authored-by: Anthony Collins <acollins1991@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect DOM structure with <caption> elements

3 participants